package org.jeecg.modules.online.cgform.enhance.impl.http;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.online.cgform.enhance.impl.http.base.CgformEnhanceHttpInter;
import org.jeecg.modules.online.cgform.entity.OnlCgformEnhanceJava;
import org.jeecg.modules.online.cgform.util.CgformUtil;
import org.springframework.stereotype.Component;

/**
 * http增强
 *
 * @author sunjianlei
 */
@Slf4j
@Component("cgformEnhanceJavaHttpImpl")
public class CgformEnhanceHttpFormImpl implements CgformEnhanceHttpInter {

    @Override
    public int execute(String tableName, JSONObject record, OnlCgformEnhanceJava enhance) {
        JSONObject params = new JSONObject();
        params.put("tableName", tableName);
        params.put("record", record);
        Object res = this.sendPost(params, enhance);
        Integer code = null;
        if (res != null) {
            code = oConvertUtils.getInt(res);
            if (code == null && res instanceof JSONObject) {
                JSONObject json = (JSONObject) res;
                code = oConvertUtils.getInt(json.get("code"));
                JSONObject newRecord = json.getJSONObject("record");
                if (newRecord != null) {
                    // 将旧值替换为新值
                    record.putAll(newRecord);
                }
            }
        }
        // return 0 = 丢弃
        // return 1 = 新增
        // return 2 = 修改
        return code == null ? CgformUtil.OL_IMPORT_INSERT : code;
    }

}
